<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Promise 示例</title>
</head>
<body>
  <p>Promise 是异步编程的一种解决方案</p>
  <p>一般情况下，是有异步操作时, 使用 <b>Promise</b> 对异步操作进行封装</p>
  <p>𓆗𓆏𓆌𓅜𓄿𓃻𓃹𓃷𓃵𓃲𓃱𓃰𓃯𓃬𓆧𓆦𓆣𓆡𓆟𓆗𓆏𓆌𓆉𓆈𓅰𓅭𓅪𓅦𓆗𓆏𓆌𓆉𓆈𓅰𓅭𓅪𓅦𓅜𓄿𓃻𓃹𓃷𓃵𓃲𓃱𓃰𓃯𓃬𓆧𓆦𓆣𓆡𓆟𓆗𓆏𓆌𓆉𓆈𓅰𓅭𓅪𓅦𓆗𓆏𓆌𓆉𓆈𓅰𓅭𓅪𓅦𓅜𓄿𓃻𓃹𓃷𓃵𓃲𓃱𓃰𓃯𓃬𓆧𓆦𓆣𓆡𓆟𓆗𓆏𓆌𓆉𓆈𓅰𓅭𓅪‘🀀🀁🀂🀃🀅🀆🀇🀈🀉🀊🀋🀌🀍🀎🀏🀐🀑🀒🀓🀔🀕🀖🀗🀘🀙</p>
</body>
<script type="text/javascript">
  // setTimeout(() => {
  //   console.log('Hello Timeout');
  // }, 1000);

  new Promise((resolve, reject) => {
    setTimeout(() => {
      console.log('Hello Timeout');
      resolve();
    }, 0);
    console.log('Hello Promise');
    // reject();
  }).then(() => {
    console.log('Bye Promise');

    /**
     * 回调地狱的处理方式
     * 通过 resolve 直接调用then里的回调方法
     */
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve({name: 'Terrdi'});
      }, 1000);
      // reject("error message");
    }).then(result => {
      console.log('Hello ', result.name);
      console.log('Hello ', result.name);
      console.log('Hello ', result.name);
      console.log('Hello ', result.name);
    });
  }).catch(message => console.warn(message));

  new Promise((resolve, reject) => {
    // resolve('success');
    Promise.resolve('success');

    // reject('error');
    return Promise.reject('error').catch(reject);
    // Promise.reject('error').catch(reject);
  }).then(result => console.log(result), error => console.warn(error));

  Promise.all([new Promise(resolve => resolve('hello')), new Promise(resolve => resolve('world'))])
      .then(results => console.log(results));
</script>
</html>
